package com.sweet.math;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;

public final class NumberUtil {
    private NumberUtil() {
    }

    /**
     * 求百分比
     *
     * @param one            分子，分子不可为0
     * @param two            分母
     * @param fractionDigits 小数位数，小数位数必须>=0
     * @param format         是否格式化 ，ture1000会显示为1,000
     * @return
     */
    public static String getPercentToFormat(float one, float two, int fractionDigits, boolean format) {
        if (two == 0.0f || fractionDigits < 0) {
            return null;
        }

        if (one == 0.0f) {
            return "0";
        }

        // 获取百分比
        double value = one / 1d / two * 100;

        String result = null;
        if (format) {
            // 创建数值格式化对象,并设置小数位数
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(fractionDigits);
            result = numberFormat.format(value);
        } else {
            // 求值后通过后缀截取
            result = String.valueOf(value);
            int index = result.indexOf(".");
            if (index != -1) {
                int pointLen = result.substring(index + 1).length();
                if (pointLen > fractionDigits) {
                    result = result.substring(0, index + 1 + fractionDigits);
                }
            }
        }

        return result;
    }

    /**
     * 计算平均值
     *
     * @param numbers
     * @param count
     * @return
     */
    public static final BigDecimal getAvg(List<BigDecimal> numbers, int count) {
        //检测范围
        if (numbers.size() < count) {
            return BigDecimal.ZERO;
        }

        //汇总
        BigDecimal total = BigDecimal.ZERO;
        for (int i = 0; i < count; i++) {
            total = total.add(numbers.get(i));
        }

        //平均值
        return total.divide(BigDecimal.valueOf(count)).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算汇总值
     *
     * @param numbers
     * @param count
     * @return
     */
    public static final BigDecimal getSum(List<BigDecimal> numbers, int count) {
        //检测范围
        if (numbers.size() < count) {
            return BigDecimal.ZERO;
        }

        //汇总
        BigDecimal total = BigDecimal.ZERO;
        for (int i = 0; i < count; i++) {
            total = total.add(numbers.get(i));
        }

        //四舍五入
        return total.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 转换数值
     *
     * @param num
     * @param defaultNum
     * @return
     */
    public static final BigDecimal toBigDecimal(String num, BigDecimal defaultNum) {
        try {
            return new BigDecimal(num);
        } catch (Exception e) {
            System.out.println(String.format("数据(%s)无法转为数字!", num));
            return defaultNum;
        }
    }

    /**
     * 保留2位小数
     *
     * @param number
     * @return
     */
    public static final BigDecimal setScale(BigDecimal number) {
        return number.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 是否相等
     *
     * @param one
     * @param two
     * @return
     */
    public static final boolean equals(BigDecimal one, BigDecimal two) {
        return one.compareTo(two) == 0;
    }
}
